package com.example.myvideoplayer.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.myvideoplayer.Bean.Video;
import com.example.myvideoplayer.R;
import com.example.myvideoplayer.Utils.VideoShowAdapter;
import com.example.myvideoplayer.presenter.VideoPresenter;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.ArrayList;

public class VideoList extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private ListView lv_video_list;
    private VideoPresenter videoPresenter;
    private QMUITipDialog tipDialog;
    private Context context;
    private final static int SCANNING_FINISH = 0;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == SCANNING_FINISH){
                tipDialog.dismiss();
                ArrayList<Video> videos = videoPresenter.getVideoList();
                VideoShowAdapter videoShowAdapter = new VideoShowAdapter(videos,context);
                lv_video_list.setAdapter(videoShowAdapter);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);
        context = this;
         tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord(getResources().getString(R.string.isScanning))
                .create();
        Log.i("video_list", "======Create: ");
        tipDialog.show();
        videoPresenter = VideoPresenter.getInstance();
        new Thread(){
            @Override
            public void run() {
                super.run();
                tipDialog.show();
                videoPresenter.scanVideo(getApplicationContext());
                handler.sendEmptyMessage(SCANNING_FINISH);
            }
        }.start();
        lv_video_list = findViewById(R.id.lv_video_show);
        lv_video_list.setOnItemClickListener(this);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        videoPresenter.setPosition(position);
        Intent i = new Intent(this,VideoPlayActivity.class);
        startActivity(i);
        Log.i("video_List", "onItemClick: "+position);
    }
    @Override
    protected void onDestroy() {
        videoPresenter.exit();
        Log.i("2222", "onDestroy: ");
        super.onDestroy();
    }

    @Override
    protected void onStop() {
        tipDialog.dismiss();
        Log.i("2222", "onStop: ");
        super.onStop();
    }

    @Override
    protected void onPause() {
        Log.i("2222", "onPause: ");
        super.onPause();
    }
}
